package com.tilaiedu.mvc.controller;

import com.tilaiedu.mvc.common.R;
import com.tilaiedu.mvc.domain.User;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;

/**
 * @Author xlzhang
 * @Date 2022/5/25 11:07
 * @Version 1.0
 * @Description TODO
 */
// @Controller
// @ResponseBody  // 代表当前的所有方法都是返回json， 环境中需要有json解析的三方依赖
@RestController  // 支持前后端分离，数据响应一定是json方式处理
// 称为RestFul开发模式接口类
public class UserController {

    // 当前是查询，表示只能功能get请求
    // http://localhost:8080/day03/users
    @PostMapping("/user")
    public R<User> getUsers(@RequestBody User user, @RequestParam String token) throws IOException {
        System.out.println("UserController.getUsers");
        System.out.println("user = " + user);
        System.out.println("token = " + token);
        // 响应行
        // 响应头
        //response.setHeader("ContentType","text/html;charset=utf-8");
        //response.setContentType("text/html;charset=utf-8");
        // 设置响应体数据
        //response.getWriter().write("查询用户数据成功");
        return R.ok("添加用户成功",user);
    }

    // http://localhost:8080/day03/user?id=1001  常规方式
    // http://localhost:8080/day03/user/1001   restFul
    // http://localhost:8080/day03/user/1001/books/5
    // @GetMapping("/user/{uid}/books/{bid}")
     @GetMapping("/user/{uid}")
    // 当使用restful风格后，参数必须使用@PathVariable
    public User getUserById(@PathVariable("uid") Integer uid){
        System.out.println("UserController.getUserById");
        System.out.println("id = " + uid);
        return new User();
    }

    // http://localhost:8080/day03/user
    // @PostMapping("/user")
    // public String addUser(User user){
    //     System.out.println("UserController.addUser");
    //     System.out.println("user = " + user);
    //     return "success";
    // }

    // 修改操作
    // http://localhost:8080/day03/user/1001
    @PutMapping("/user/{uid}")
    public R<Object> updateUser(@PathVariable Integer uid, @RequestBody User user){
        System.out.println("UserController.updateUser");
        System.out.println("uid = " + uid);
        user.setLike("这是修改后的用户对象");
        System.out.println("user = " + user);
        return R.ok("修改用户成功");
    }

    // 删除操作
    // http://localhost:8080/day03/user/1001
    @DeleteMapping("/user/{uid}")
    public R<Object> deleteUser(@PathVariable Integer uid){
        System.out.println("UserController.deleteUser");
        System.out.println("uid = " + uid);
        return R.ok("删除成功");
    }

    // 删除操作
    // http://localhost:8080/day03/user/1,2,3,45
    @DeleteMapping("/users/{uid}")
    public String deleteUser(@PathVariable("uid") Integer[] uids){
        System.out.println("UserController.deleteUser");
        System.out.println("uids = " + Arrays.toString(uids));
        return "success";
    }
}











